package com.cardone.platform.common.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.common.dto.EntityDefaultDto;

/**
 * 实体与默认
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class EntityDefaultJdbcDao implements EntityDefaultDao {
	@Override
	public int deleteByIds(final EntityDefaultDto deleteEntityDefault) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteEntityDefault.getAttrs())) {
			model.putAll(deleteEntityDefault.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteEntityDefault.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final EntityDefaultDto findEntityDefault) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityDefault, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityDefault, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findEntityDefault.getAttrs())) {
			model.putAll(findEntityDefault.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final EntityDefaultDto findEntityDefault) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityDefault);

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityDefault, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findEntityDefault.getAttrs())) {
			model.putAll(findEntityDefault.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final EntityDefaultDto findListEntityDefault) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListEntityDefault).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListEntityDefault.getAttrs())) {
			model.putAll(findListEntityDefault.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final EntityDefaultDto insertEntityDefault) {
		if (StringUtils.isBlank(insertEntityDefault.getId())) {
			insertEntityDefault.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertEntityDefault));

		if (MapUtils.isNotEmpty(insertEntityDefault.getAttrs())) {
			model.putAll(insertEntityDefault.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final EntityDefaultDto paginationEntityDefault) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationEntityDefault).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationEntityDefault.getAttrs())) {
			model.putAll(paginationEntityDefault.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationEntityDefault), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final EntityDefaultDto readEntityDefault) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readEntityDefault, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readEntityDefault.getAttrs())) {
			model.putAll(readEntityDefault.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readEntityDefault);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readEntityDefault, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final EntityDefaultDto saveEntityDefault) {
		final P oldEntityDefault = this.findByCodeOrId(mappedClass, saveEntityDefault);

		if (oldEntityDefault == null) {
			this.insertByCode(saveEntityDefault);

			return this.findByCodeOrId(mappedClass, saveEntityDefault);
		}

		final int updateCount = this.updateByCode(saveEntityDefault, oldEntityDefault);

		if (updateCount < 1) {
			return oldEntityDefault;
		}

		return this.findByCodeOrId(mappedClass, saveEntityDefault);
	}

	@Override
	public int updateByCode(final EntityDefaultDto updateEntityDefault) {
		final EntityDefaultDto oldEntityDefault = this.findByCodeOrId(EntityDefaultDto.class, updateEntityDefault);

		return this.updateByCode(updateEntityDefault, oldEntityDefault);
	}

	private <P> int updateByCode(final EntityDefaultDto updateEntityDefault, final P oldEntityDefault) {
		if (oldEntityDefault == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateEntityDefault);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateEntityDefault, oldEntityDefault, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldEntityDefault, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldEntityDefault, updateEntityDefault, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateEntityDefault, updateProperties), new ModelArgs(updateEntityDefault, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateEntityDefault.getAttrs())) {
			model.putAll(updateEntityDefault.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}